package com.stormrage.shiro.taglib;

import org.springframework.util.CollectionUtils;

import com.stormrage.spring.SpringUtils;
import com.stormrage.edu.serv.shiro.ShiroServ;
import com.stormrage.entity.shiro.ShiroRes;

import java.util.Collection;

/**
 * @author www.stormragetech.com
 * @date 2016年1月19日
 * @version 1.0.0
 */
public class Functions {

	@SuppressWarnings("rawtypes")
	public static boolean in(Iterable iterable, Object element) {
		if (iterable == null) {
			return false;
		}
		return CollectionUtils.contains(iterable.iterator(), element);
	}

	public static String resourceName(String id) throws Exception {
		ShiroRes res = getShiroServ().queryResById(id);
		if (res == null) {
			return "";
		}
		return res.getRes();
	}

	public static String resourceNames(Collection<String> ids) throws Exception {
		if (CollectionUtils.isEmpty(ids)) {
			return "";
		}

		StringBuilder s = new StringBuilder();
		for (String id : ids) {
			ShiroRes res = getShiroServ().queryResById(id);
			if (null == res) {
				return "";
			}
			s.append(res.getRes());
			s.append(",");
		}

		if (s.length() > 0) {
			s.deleteCharAt(s.length() - 1);
		}

		return s.toString();
	}

	private static ShiroServ shiroServ;

	public static ShiroServ getShiroServ() {
		if (null == shiroServ) {
			shiroServ = SpringUtils.getBean(ShiroServ.class);
		}
		return shiroServ;
	}

}